package com.sports.common.util;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.base.Preconditions;
import com.sports.common.constant.ResCode;
import com.sports.common.exception.AppException;

import java.util.Collection;

/**
 * @ClassName ConditionUtil
 * @Desciption
 * @Author MaRui
 * @address https://maruinotes.com
 * @Date 2021/11/20 13:37
 * @Version 1.0
 */
public class ConditionUtil {

    public static void checkNotNull(Object object, String msg) {
        try {
            Preconditions.checkNotNull(object, msg);
        } catch (NullPointerException e) {
            throw new AppException(ResCode.PARAM_VALID.getValue() + ": " + e.getMessage(), ResCode.PARAM_VALID.code());
        }
    }

    public static void checkArgument(Boolean bo, String msg) {
        try {
            Preconditions.checkArgument(bo, msg);
        } catch (IllegalArgumentException e) {
            throw new AppException(ResCode.PARAM_VALID.getValue() + ": " + e.getMessage(), ResCode.PARAM_VALID.code());
        }
    }

    public static void validCollectionIsEmpty(Collection<?> collection, String msg) {
        if (CollectionUtils.isNotEmpty(collection)) {
            throw new AppException(ResCode.PARAM_VALID.getValue() + ": " + msg, ResCode.PARAM_VALID.code());
        }
    }

    public static void validCollectionNotEmpty(Collection<?> collection, String msg) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new AppException(ResCode.PARAM_VALID.getValue()+": " + msg, ResCode.PARAM_VALID.code());
        }
    }
}
